package com.cssl.controller;

import cn.hutool.core.util.ObjectUtil;
import com.cssl.domain.DtsCategory;
import com.cssl.domain.dto.DtsCategoryDto;
import com.cssl.service.DtsCategoryService;
import com.cssl.util.InfoCode;
import com.cssl.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/category")
@Api(value = "分类接口",tags = "分类相关操作")
public class DtsCategoryController {

    @Autowired
    private DtsCategoryService categoryService;

    /**
     * 查询三级分类商品位于goods controller层
     * @param map id
     * @return
     */
    @PostMapping("/getLevel2")
    @ApiOperation(value = "获取二级分类",notes = "id")
    public Result getLevel2(@RequestBody Map map) {
        List<DtsCategory> l2 = categoryService.getCategory("L2", Integer.parseInt(map.get("id").toString()));
        if (l2 != null && l2.size() > 0) {
            return Result.result(InfoCode.OK, InfoCode.MESSAGE_OK, l2, System.currentTimeMillis());
        }
        return Result.fail(InfoCode.QUERY_EMPTY, InfoCode.MESSAGE_QUERY_EMPTY, System.currentTimeMillis());
    }

    /**
     * 一,二级分类查询
     *
     * @param
     * @param
     * @return
     */
    @PostMapping("/getCategory")
    @ApiOperation(value = "获取一级分类或二级分类",notes = "levels,id")
    public Result getCategory(@RequestBody Map map) {
        List<DtsCategory> category = categoryService.getCategory(map.get("levels").toString(),
                Integer.parseInt(map.get("id").toString()));
        if (category != null && category.size() > 0) {
            return Result.result(InfoCode.OK, InfoCode.MESSAGE_OK, category, System.currentTimeMillis());
        }
        return Result.fail(InfoCode.QUERY_EMPTY, InfoCode.MESSAGE_QUERY_EMPTY, System.currentTimeMillis());
    }


    @PostMapping("/getL1AndL2")
    @ApiOperation(value = "获取一级分类及对应的二级分类")
    public Result getL1AndL2(){
        return  categoryService.getL1AndL2();
    }

    @PostMapping("/clickL1GetL2")
    @ApiOperation(value = "获取选中的一级分类及二级分类")
    public Result clickL1GetL2(@RequestBody DtsCategory category){
        return  categoryService.clickL1GetL2(category.getId());
    }

    @PostMapping("/getLevel2AndCheckedL2")
    @ApiOperation(value = "获取选中的二级分类及选中的二级分类", notes = "id,pId")
    public Result getLevel2AndCheckedL2(@RequestBody DtsCategory category){
        return categoryService.getLevel2AndCheckedL2(category);
    }
    /**
     * 后台商品类目显示
     */
    @PostMapping("/list")
    Result dtsorderlist(@RequestBody DtsCategoryDto dtsCategoryDto){
        Map categoylist=categoryService.findPagecategory(dtsCategoryDto);
        if (categoylist==null|| ObjectUtil.isEmpty(categoylist.get("list"))) {
            return Result.result(InfoCode.QUERY_EMPTY, InfoCode.MESSAGE_QUERY_EMPTY, null, System.currentTimeMillis());
        }
        return Result.result(InfoCode.OK,InfoCode.MESSAGE_OK,categoylist,System.currentTimeMillis());
    }

    @DeleteMapping("/delete")
    Result categorydelete(@RequestParam("ids") String ids){
        boolean branddelete=categoryService.categorydelete(ids);
        if(branddelete){
            return Result.result(InfoCode.OK, "删除成功", null, System.currentTimeMillis());
        }else {
            return Result.result(InfoCode.FAILED, "删除失败", null, System.currentTimeMillis());
        }
    }
    @PostMapping("/updatecategory")
    Result updatecategory(@RequestBody Map updatecategory){
        int brandadd=categoryService.updatecategory(updatecategory);
        System.out.println("信息"+updatecategory.size());
        return Result.result(InfoCode.OK,InfoCode.MESSAGE_OK,brandadd,System.currentTimeMillis());
    }
    @PostMapping("/addcategory")
    Result addcategory(@RequestBody Map addcategory){
        int brandadd=categoryService.addcategory(addcategory);
        System.out.println("信息"+addcategory.size());
        return Result.result(InfoCode.OK,InfoCode.MESSAGE_OK,brandadd,System.currentTimeMillis());
    }

}
